﻿using System.Web;

namespace NWebMock
{
    public static class HttpContextManager
    {
        #region Constants

        private const string CONTEXT_ITEM_KEY = "ABSTRACT_CONTEXT";

        #endregion

        #region Properties

        public static IHttpContext Current
        {
            get
            {
                // This property accessor will check to see if an existing instance 
                // has been created for the current context.  The item will be
                // stored in the context's items collection so that it will be
                // garbage collected with the context.

                HttpContext context = HttpContext.Current;

                if (context.Items.Contains(CONTEXT_ITEM_KEY))
                {
                    return (IHttpContext)context.Items[CONTEXT_ITEM_KEY];
                }
                else
                {
                    System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
                    timer.Start();
                    IHttpContext item = new HttpContextWrapper(context);
                    timer.Stop();

                    long len = timer.ElapsedMilliseconds;
                    context.Items.Add(CONTEXT_ITEM_KEY, item);

                    return item;
                }
            }
        }

        #endregion
    }
}
